//
//  TDHandleImageViewController.m
//  TotalDemo
//
//  Created by gujian on 16/6/13.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "TDHandleImageViewController.h"
#import "TDChooseView.h"

#define DEGREES_TO_RADIANS(radians) (M_PI * radians/180.0)
#define H_PATTERN_SIZE 16
#define V_PATTERN_SIZE 18

@interface TDHandleImageViewController()

@property (nonatomic, weak) TDChooseView *chooseView;

@end


@implementation TDHandleImageViewController

//- (void)loadView
//{
//    
//}


- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"图像处理";
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        
    }
    [self setupSubviews];
    
    [self initRightBarButtonItem];
}

- (void)setupSubviews
{
    CGFloat width = 100;
    CGFloat height = 100;
    TDChooseView *chooseView = [[TDChooseView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.0-width/2.0, SCREEN_HEIGHT/2.0-height/2.0, width, height)];
    [self.view addSubview:chooseView];
    _chooseView = chooseView;
    
    [self addImageView];
}

- (void)initRightBarButtonItem
{
    UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"动画" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBarBtnItem;
}

- (void)rightBarButtonAction:(id)sender
{
    
//    [self testMixAnimation];
//    [self testAutorelease];
//    [self testNestedAnimation];
    
//    [self testMixAlpha];
    [self testPattern];
}

- (void)addImageView
{
    UIImageView *animatedImgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 30, 66)];
    [self.view addSubview:animatedImgView];
    
    NSMutableArray *imageArr = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 10; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"number_black_%@",@(i)]];
        [imageArr addObject:image];
    }

    UIImage *animatedImage = [UIImage animatedImageWithImages:imageArr duration:2.0];
    animatedImgView.image = animatedImage;
    
    
}

- (void)testPattern
{
    CGRect rect = CGRectMake(100, 100, 100, 100);
    
     UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
//    MyColoredPatternPainting(context, rect);
    
    
}

//void MyDrawColoredPattern (void *info, CGContextRef myContext)
//{
//    CGFloat subunit = 5;
//    
//    CGRect myRect1 = {{0,0},{subunit, subunit}},
//           myRect2 = {{subunit,subunit}, {subunit,subunit}},
//           myRect3 = {{0,subunit},subunit,subunit},
//           myRect4 = {{subunit,0},{subunit,subunit}};
//    
//    CGContextSetRGBFillColor(myContext, 0, 0, 1, 0.5);
//    CGContextFillRect(myContext, myRect1);
//    
//    CGContextSetRGBFillColor(myContext, 1, 0, 0, 0.5);
//    CGContextFillRect(myContext, myRect2);
//    
//    CGContextSetRGBFillColor(myContext, 0, 1, 0, 0.5);
//    CGContextFillRect(myContext, myRect3);
//    
//    CGContextSetRGBFillColor(myContext, .5, 0, .5, 0.5);
//    CGContextFillRect(myContext, myRect4);
//    
//    
//}
//
//void MyColoredPatternPainting(CGContextRef mycontext, CGRect rect)
//{
//    CGPatternRef pattern;
//    CGColorSpaceRef patternSpace;
//    CGFloat alpha = 1,
//            width,height;
//    
//    static const CGPatternCallbacks callbacks = {0,&MyDrawColoredPattern,NULL};
//    
//    CGContextSaveGState(mycontext);
//    patternSpace = CGColorSpaceCreatePattern(NULL);
//    CGContextSetFillColorSpace(mycontext, patternSpace);
//    CGColorSpaceRelease(patternSpace);
//    
//    pattern = CGPatternCreate(NULL, CGRectMake(0, 0, H_PATTERN_SIZE, V_PATTERN_SIZE), CGAffineTransformMake(1, 0, 0, 1, 0, 0), H_PATTERN_SIZE, V_PATTERN_SIZE, kCGPatternTilingConstantSpacing, true, &callbacks);
//    
//    CGContextSetFillPattern(mycontext, pattern, &alpha);
//    CGPatternRelease(pattern);
//    CGContextFillRect(mycontext, rect);
//    CGContextRestoreGState(mycontext);
//    
//}


/**
 *  In the normal blend mode (which is the default for the graphics state) Quartz performs alpha blending by combining the components of the source color with the components of the destination color using the formula
 
 destination = (alpha * source) + (1 - alpha) * destination
 
 where source is one component of the new paint color and destination is one component of the background color. This formula is executed for each newly painted shape or image
 
 detail to see:
 https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_color/dq_color.html#//apple_ref/doc/uid/TP30001066-CH205-TPXREF101
 
 */
- (void)testMixAlpha
{

    CGFloat offX = 25;
    CGFloat offY = 100;
    
    for (int i = 0; i < 6; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(offX, offY, 100, 100)];
        view.alpha = i / 5.0;
        view.backgroundColor = [UIColor redColor];
        
        UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 50, 50)];
        subView.backgroundColor = [UIColor yellowColor];
        [view addSubview:subView];
        
        [self.view addSubview:view];
        
        
        if ((i+1) % 3 == 0) {
            offX = 25;
            offY+= 110;
            
        }else
        {
            offX += 110;
        }
        
    }
    
}

- (void)testMixAnimation
{
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        CGAffineTransform xform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-180));
        _chooseView.transform = xform;

        //Rotate the embedded CALayer in the oppsite direction
        CABasicAnimation *layerAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
        layerAnimation.duration = 2.0;
        layerAnimation.beginTime = 0;
        layerAnimation.valueFunction = [CAValueFunction functionWithName:kCAValueFunctionRotateZ];
        layerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        layerAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        layerAnimation.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(360.0)];
        [_chooseView.layer addAnimation:layerAnimation forKey:@"layerAnimation"];


    } completion:^(BOOL finished) {

        //Now do the second half of the view rotation
        [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            CGAffineTransform xform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-359));
            _chooseView.transform = xform;
        } completion:^(BOOL finished) {
            _chooseView.transform = CGAffineTransformIdentity;
        }];
        
    }];
}

- (void)testNestedAnimation
{
   __block NSDate *date = [NSDate date];

    [UIView animateWithDuration:1.0 animations:^{
        _chooseView.alpha = 0.0;

        date = [NSDate date];
        NSLog(@"currentTime1b:%@",@([date timeIntervalSince1970]));

        [UIView animateWithDuration:0.2 animations:^{

             date = [NSDate date];
             NSLog(@"currentTime2b:%@",@([date timeIntervalSince1970]));

            [UIView animateWithDuration:2.5 animations:^{
                 _chooseView.alpha = 0.0;

                 date = [NSDate date];
                NSLog(@"currentTime3b:%@",@([date timeIntervalSince1970]));

            } completion:^(BOOL finished) {

                 date = [NSDate date];
                 NSLog(@"currentTime3e:%@",@([date timeIntervalSince1970]));
            }];


        } completion:^(BOOL finished) {

             date = [NSDate date];
             NSLog(@"currentTime2e:%@",@([date timeIntervalSince1970]));
        }];


    } completion:^(BOOL finished) {
        
         date = [NSDate date];
         NSLog(@"currentTime1e:%@",@([date timeIntervalSince1970]));
    }];
}

- (void)testAutorelease
{
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];

    NSDate *beingDate = [NSDate date];

    for (int i = 0; i < 100000; i++) {

        @autoreleasepool {

        UIImage *image = [UIImage imageNamed:@"cat.jpg"];

        CGSize size = CGSizeMake(100, 100);
        CGRect rect = CGRectMake(100, 100, 100, 100);
        CGContextRef context =  UIGraphicsGetCurrentContext();

        UIGraphicsBeginImageContextWithOptions(size, NO, 0);

        CGContextDrawImage(context, rect, image.CGImage);

        UIImage *targetImage = UIGraphicsGetImageFromCurrentImageContext();

        CGContextRelease(context);

        [tempArr addObject:targetImage];

        }
    }

    NSDate *endDate = [NSDate date];
    
    NSLog(@"******%f",([endDate timeIntervalSince1970] - [beingDate timeIntervalSince1970]));
}



@end
